package ewms.controller;

import java.util.List;
import java.util.Arrays;

import ewms.bo.EwmsWarehouseAddBo;
import ewms.bo.EwmsWarehouseEditBo;
import ewms.bo.EwmsWarehouseQueryBo;
import ewms.domain.EwmsWarehouse;
import ewms.service.IEwmsWarehouseService;
import ewms.vo.EwmsWarehouseVo;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import ewms.common.annotation.RepeatSubmit;
import ewms.common.annotation.Log;
import ewms.common.core.controller.BaseController;
import ewms.common.core.domain.AjaxResult;
import ewms.common.enums.BusinessType;
import ewms.common.utils.poi.ExcelUtil;
import ewms.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 仓库信息Controller
 *
 * @author zhaoxiaochun
 * @date 2022-01-11
 */
@Api(value = "仓库信息控制器", tags = {"仓库信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/warehouse")
public class EwmsWarehouseController extends BaseController {

    private final IEwmsWarehouseService iEwmsWarehouseService;

    /**
     * 查询仓库信息列表
     */
    @ApiOperation("查询仓库信息列表")
    @PreAuthorize("@ss.hasPermi('system:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo<EwmsWarehouseVo> list(@Validated EwmsWarehouseQueryBo bo) {
        return iEwmsWarehouseService.queryPageList(bo);
    }

	/**
	 * 查询仓库信息列表
	 * @return
	 */
	@ApiOperation("查询仓库信息列表")
//	@PreAuthorize("@ss.hasPermi('system:warehouse:list')")
	@GetMapping("/listForSelect")
	public AjaxResult<List<EwmsWarehouse>> listForSelect() {
		return AjaxResult.success(iEwmsWarehouseService.list());
	}

    /**
     * 导出仓库信息列表
     */
    @ApiOperation("导出仓库信息列表")
    @PreAuthorize("@ss.hasPermi('system:warehouse:export')")
    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EwmsWarehouseVo> export(@Validated EwmsWarehouseQueryBo bo) {
        List<EwmsWarehouseVo> list = iEwmsWarehouseService.queryList(bo);
        ExcelUtil<EwmsWarehouseVo> util = new ExcelUtil<EwmsWarehouseVo>(EwmsWarehouseVo.class);
        return util.exportExcel(list, "仓库信息");
    }

    /**
     * 获取仓库信息详细信息
     */
    @ApiOperation("获取仓库信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:warehouse:query')")
    @GetMapping("/{warehouseId}")
    public AjaxResult<EwmsWarehouseVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("warehouseId") Long warehouseId) {
        return AjaxResult.success(iEwmsWarehouseService.queryById(warehouseId));
    }

    /**
     * 新增仓库信息
     */
    @ApiOperation("新增仓库信息")
    @PreAuthorize("@ss.hasPermi('system:warehouse:add')")
    @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody EwmsWarehouseAddBo bo) {
        return toAjax(iEwmsWarehouseService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改仓库信息
     */
    @ApiOperation("修改仓库信息")
    @PreAuthorize("@ss.hasPermi('system:warehouse:edit')")
    @Log(title = "仓库信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody EwmsWarehouseEditBo bo) {
        return toAjax(iEwmsWarehouseService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除仓库信息
     */
    @ApiOperation("删除仓库信息")
    @PreAuthorize("@ss.hasPermi('system:warehouse:remove')")
    @Log(title = "仓库信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{warehouseIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] warehouseIds) {
        return toAjax(iEwmsWarehouseService.deleteWithValidByIds(Arrays.asList(warehouseIds), true) ? 1 : 0);
    }
}
